from time import sleep

from selenium import webdriver
'''
关键字驱动：将常用的函数进行提取，再做二次封装，实现一个工具库的生成，在做自动化测试中，
通过调用工具库，来实现自动化测试的效果
'''
class TestKeyWords(object):
    # 初始化
    def __init__(self,browser_type,url):
        self.driver=self.open_browser(browser_type)
        self.driver.get(url)

    # 调用浏览器
    def open_browser(self,browser_type):
        if browser_type == 'chrome':
            driver = webdriver.Chrome()
            return driver
        elif browser_type == 'firefox':
            driver = webdriver.Firefox()
            return driver
        else:
            print('type error')

    # 元素定位
    def locator(self,locator_type,value):
        if locator_type == 'xpath':
            el = self.driver.find_element_by_xpath(value)
            return el
        elif locator_type == 'id':
            el = self.driver.find_element_by_id(value)
            return el
        elif locator_type == 'name':
            el = self.driver.find_element_by_name(value)
            return el

    #输入
    def input_text(self,locator_type,value,text):
        self.locator(locator_type,value).send_keys(text)

    # 点击
    def click_element(self,locator_type,value):
        self.locator(locator_type,value).click()

    # 关闭浏览器
    def quit_browser(self):
        self.driver.quit()


if __name__ == '__main__':
    tk = TestKeyWords('chrome','http://www.baidu.com/')
    sleep(1)
    tk.input_text('id','kw','你好')
    tk.click_element('id','su')
    tk.quit_browser()


